package com.xiaofengstu.mcams.web.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.zxing.WriterException;
import com.xiaofengstu.mcams.dto.AssetDTO;
import com.xiaofengstu.mcams.util.QRCodeGenerator;
import com.xiaofengstu.mcams.util.ThreadLocalUtils;
import com.xiaofengstu.mcams.web.entity.TAsset;
import com.xiaofengstu.mcams.web.entity.TUser;
import com.xiaofengstu.mcams.web.entity.Workflow;
import com.xiaofengstu.mcams.web.mapper.TAssetMapper;
import com.xiaofengstu.mcams.web.service.TAssetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofengstu.mcams.web.service.TUserService;
import com.xiaofengstu.mcams.web.service.WorkflowService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fengzeng
 * @since 2022-05-07
 */
@Service
@RequiredArgsConstructor
public class TAssetServiceImpl extends ServiceImpl<TAssetMapper, TAsset> implements TAssetService {

  private final TAssetMapper assetMapper;
  private final TUserService userService;

  private final WorkflowService workflowService;
  private final QRCodeGenerator qrCodeGenerator;



  @Value("${file.qrcode}")
  private String filePath;


  @Override
  public Page<AssetDTO> getAssetList(Page page) {
    return assetMapper.getAssetList(page);
  }

  @Override
  public TAsset add(TAsset asset) {
    Integer userId = ThreadLocalUtils.get();
    TUser user = userService.getById(userId);
    asset.setCampusId(user.getCampusId());
    asset.setDeptId(user.getDeptId());
    asset.setStatus("待入库");
    asset.setUserId(userId);
    asset.setStatusStep(3);
    assetMapper.insert(asset);
    return asset;
  }

  @Override
  public Boolean buyApply(TAsset asset) {
    Integer userId = ThreadLocalUtils.get();
    TUser user = userService.getById(userId);
    asset.setUserId(userId);
    asset.setStatus("一级审批");
    asset.setCampusId(user.getCampusId());
    asset.setDeptId(user.getDeptId());
    assetMapper.insert(asset);


    Workflow workflow = Workflow.builder()
        .workflowType("资产申购")
        .status("一级审批")
        .createBy(userId)
        .assetId(asset.getAssetId())
        .applyReason(asset.getApplyReason()).build();

    return workflowService.save(workflow);
  }

  @Override
  public String generQRCode(TAsset asset) throws IOException, WriterException {
    String suffix = System.currentTimeMillis() + ".PNG";

    qrCodeGenerator.generateQRCodeImage(asset.toString(), filePath + "\\" + suffix);

    return "/img/" + suffix;
  }

  @Override
  public List<Map<String,Integer>> getDates() {
    List<Map<String,Integer>> dates = assetMapper.getDates();
    return dates;
  }

  @Override
  public List<Map<String, Integer>> getValues() {
    List<Map<String,Integer>> values = assetMapper.getValues();
    return values;
  }


}
